//
//  Date.swift
//  Enchanted
//
//  Created by Augustinas Malinauskas on 10/12/2023.
//

import Foundation

extension Date {
    func daysAgoString() -> String {
        let calendar = Calendar.current
        let now = Date()
        let components = calendar.dateComponents([.day], from: self, to: now)
        
        guard let daysAgo = components.day else {
            return "Today"
        }
        
        switch daysAgo {
        case 0:
            return "Today"
        case 1:
            return "1 day ago"
        default:
            return "\(daysAgo) days ago"
        }
    }
    
    public func format(_ format: String = "yyyy-MM-dd HH:mm:ss") -> String {
        let dfmatter = DateFormatter()
        dfmatter.dateFormat = format
        return dfmatter.string(from: self)
    }
    
    /// 时间戳字符串
    public static var tspString: String {
        let tmp = Date(timeIntervalSinceNow: 0).timeIntervalSince1970
        return String(tmp).components(separatedBy: ".")[0]
    }
    /// 当前时间戳
    public static var currentTime: TimeInterval {
        return Date(timeIntervalSinceNow: 0).timeIntervalSince1970
    }
    
    public func jmIsToday() -> Bool {
        return Calendar.current.isDateInToday(self)
    }
}
